/**
 * 公告管理
 */
Ext.define('Module.System.Gsgg', {
	extend : 'Ext.panel.Panel',
	layout: 'border',
	initComponent : function() {
		var me = this;
		var store=Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields: ['id','title','nr','image','upuser','uptime','name'],
	        proxy: {
	            type: 'ajax',
	            url: 'GsggAction/getGsgg.do',
	            reader: {
					root: 'datas',
					totalProperty: 'total'
				}
	        }
		});
		
		var gridPanel = me.gridPanel = Ext.create('Ext.grid.Panel',{
			title: '公告列表',
			region: 'center',
			padding: '0 1 0 0',
//			width: 650,
			store: store,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: '100%',
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [{
						xtype: 'toolbar',
						items: [{
							text: '添加公告',
							iconCls: 'icon-add',
							handler: function(){
								me.addWgSyjs();
							}
						},{
							text: '修改公告',
							iconCls: 'icon-edit',
							handler: function(){
								var rec = gridPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.updateWgSyjs(rec);
									Ext.getCmp(me.id+'-file').allowBlank=true;
								}
								else{
									Ext.Msg.alert('提示', '请选择要修改的公告！');
								}
							}
						},{
							text: '删除公告',
							iconCls: 'icon-delete',
							handler: function(){
								var rec = gridPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.delWgSyjs(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要删除的公告！');
								}
							}
						},{
							xtype:"textfield",
							fieldLabel : '标题',	
							maxLength:40,
							labelWidth:40,
							width:220
						},{
							text:"查询",
							iconCls: 'icon-search',
							handler: function(){
								var tbar = this.up('toolbar');
								var items = tbar.items.items;
								gridPanel.store.proxy.extraParams = {
									title: items[3].getSubmitValue(),
								};
								gridPanel.store.loadPage(1);
							}
						}
					]
				 }]
			}],		
			bbar: Ext.create('Ext.PagingToolbar', {
	            store: store,
	            id: me.id+'-bbar',
	            displayInfo: true,
	            displayMsg: '显示第 {0} 至 {1} 条记录，共 {2} 条',
	            emptyMsg: "无记录"
	        }),
			columns: [{
	            text: '标题',
	            flex: 2.5,
	            dataIndex: 'title'
			},{
	            text: '内容',
	            flex: 2,
	            dataIndex: 'nr'
			},{
	            text: '发送人',
	            flex: 2,
	            dataIndex: 'name'
			},{
	            text: '发送时间',
	            flex: 2.6,
	            dataIndex: 'uptime',
	            renderer: formatDate
			}],
			listeners:{
				itemdblclick:function(s,rec,item){
					var uptime=Ext.Date.format(new Date(rec.data.uptime),'Y-m-d');
					me.getGsggWin(rec.data.id,rec.data.title,rec.data.nr,rec.data.name,uptime);
				}
			}
			
		});
		
		
		me.items = [gridPanel];
		
		me.callParent();
	},
	updateWgSyjs: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 850,
			height: 450,
			iconCls: 'icon-edit',
			modal: true,
			layout: 'fit',
			title: '修改公告',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'GsggAction/updateGsgg.do?r='+Math.random(),
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		formp.up('window').close();
            	    	    		me.gridPanel.store.load();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '修改失败！详情：<br>'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的公告,请正确填写公告！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 10,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			},rec.data.id)
		}).show();
	},
	addWgSyjs: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 850,
			height: 470,
			iconCls: 'icon-add',
			modal: true,
			layout: 'fit',
			title: '添加公告',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'GsggAction/addGsgg.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		formp.up('window').close();
            	    	    		me.gridPanel.store.load();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '保存失败！详情：<br>'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的公告,请正确填写公告！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	delWgSyjs: function(rec){
		var me = this;
		if(rec){
			Ext.Msg.confirm('提示', '你确定要删除当前公告吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'GsggAction/deleteGsgg.do',
					    params: rec.data,
					    success: function(response){
        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	if(rs.rst){
        	    	    		Ext.Msg.alert('提示', '删除成功！');
        	    	    		me.gridPanel.store.load();
        	    	    	}
        	    	    	else{
        	    	    		Ext.Msg.alert('提示', '删除失败！详情：<br>'+rs.msg);
        	    	    	}
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的公告！');
		}
	},
	getForm: function(config,id){
		var me=this;
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			fileUpload: true, 
			defaults: {anchor: '100%'},
			items: [{
					name: 'id',
					xtype: 'hidden'
					
				},{
					fieldLabel: '公告标题',
					name: 'title',
					maxLength:40,
					allowBlank: false,
					xtype: 'textfield'
				},{
					xtype: 'textarea',
					fieldLabel: '公告内容',
					maxLength:2000,
					name: 'nr',
					allowBlank: false
				},{
					fieldLabel: '公告图片',
					xtype: 'fileuploadfield',
					name: 'file',
					id:me.id+'-file',
					buttonText: '选择文件', 
					allowBlank: false,
					preview: function(){
						var f = this;
						var url;
			    		var p;
			    		var fieldset = f.ownerCt.down('fieldset');
						if (navigator.userAgent.indexOf("MSIE")>=1) { // IE
							var dom = $('input[type=file]',f.el.dom)[0];
							dom.select();
			                var imgSrc = document.selection.createRange().text;
			                var img = $('img',fieldset.el.dom)[0];
			                $('img',fieldset.el.dom).attr('src',Ext.BLANK_IMAGE_URL);
			                try{
			                	img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
			                	img.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
			                }
			                catch(e)
			                {}
			                document.selection.empty();
						} else if(navigator.userAgent.indexOf("Firefox")>0) { // Firefox
							p = f.fileInputEl.dom.files.item(0);
							url = window.URL.createObjectURL(p);
							$('img',fieldset.el.dom).attr('src',url);
						} else if(navigator.userAgent.indexOf("Chrome")>0) { // Chrome
							p = f.fileInputEl.dom.files.item(0);
							url = window.URL.createObjectURL(p);
							$('img',fieldset.el.dom).attr('src',url);
						}
					},
					listeners: {
						change: function(f,value,ov){
							f.preview();
						},
						enable: function(f){
							f.preview();
						}
					}
				},{
					xtype:"displayfield",
					fieldLabel: '<lable style="color:red;">温馨提醒：请上传宽度950px,高度220px的图片</lable>',
					height: 20,
					labelWidth:'100%',
					labelSeparator:''
				},{
					xtype: 'fieldset',
					height: 220,
					title: '图片预览',
					id:me.id+'gsggImg',
					html: '<img style="margin-bottom:20px;" src="'+Ext.BLANK_IMAGE_URL+'" height=180 width=100%></img>',
					listeners:{
						afterrender:function(fieldset,eOpts) {
							if(id !=null){
								$('img',fieldset.el.dom).attr('src','GsggAction/getGsggImg.do?r='+Math.random()+'&id='+id);
							}
						}
					}
					
				},
			]
		},config);
	},
	getGsggWin: function(id,title,nr,name,uptime){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 1000,
			height: 500,
			iconCls: 'icon-edit',
			modal: true,
			layout: 'fit',
			title: '公告信息',
			buttons: [{
				text: '关闭',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: {
					xtype: 'panel',
					autoScroll: true,
					border:false,
					bodyStyle: {
					     backgroundColor:'white'
			        },
					html:'<div style="margin:20px;background-color:white;">'
								+'<div style="font-size: 26px;font-weight: bold;width:100%;text-align:center;">'
									+'<lable>'+title+'</lable>'
								+'</div>'
								+'<div style="font-size: 12px;width:28%;float:right;margin-top:5px;">'
									+'<lable>来自：'+name+'</lable><lable style="margin-left:15px;">'+uptime+'</lable>'
								+'</div>'
								+'<div style="width:100%;margin-top:30px;">'	
									+'<img width="100%" height="250px" src="GsggAction/getGsggImg.do?id='+id+'"/>'
								+'</div>'
								+'<div style="margin-top:20px;display:block;word-break: break-all;word-wrap: break-word;">'
									+nr+''
								+'</div>'
							+'</div>'
			}
		}).show();
		
    }
});



